const Koa = require('koa');
const Router = require('koa-router');
const views = require('koa-views');
const mysql2 = require('mysql2');

const app = new Koa();
const router = new Router();

const connection = mysql2.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'test'
});

app.use(views(__dirname, {
  extension: 'html'
}))

router.get('/', (ctx, next) => {
  ctx.redirect('/index');
})

router.get('/msg', async ctx => {
  let [rows] = await connection.promise().query('SELECT msg FROM message');
  ctx.body = rows;
})

router.get('/index', async (ctx, next) => {
  await ctx.render('index.html');
})

app.use(router.routes());

app.listen(8888);